﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;

namespace FDeal
{
    public class ThongTinTaiKhoanDAO : DataProvider
    {
        public ThongTinTaiKhoanDAO()
        { 
        }

        public ThongTinTaiKhoanDTO[] getDsThongTinTaiKhoan()
        {
            connect();
            string query = "SELECT * FROM THONGTINTAIKHOAN";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ThongTinTaiKhoanDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new ThongTinTaiKhoanDTO[n];
            for (i = 0; i < n; i++)
            {
                object lop = GetDataFromDataRow(dt, i);
                arr[i] = (ThongTinTaiKhoanDTO)lop;
            }
            return arr;
        }



        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ThongTinTaiKhoanDTO tt = new ThongTinTaiKhoanDTO();
            tt.Idtaikhoan = dt.Rows[i]["IDTAIKHOAN"].ToString();
            tt.Tendangnhap = dt.Rows[i]["TENDANGNHAP"].ToString();
            tt.Email = dt.Rows[i]["EMAIL"].ToString();
            tt.Matkhau = dt.Rows[i]["MATKHAU"].ToString();
            tt.Quocgia = dt.Rows[i]["QUOCGIA"].ToString();
            tt.Tinhthanh = dt.Rows[i]["TINHTHANH"].ToString();
            tt.Gioitinh = dt.Rows[i]["GIOITINH"].ToString();
            tt.Ngaysinh = DateTime.Parse(dt.Rows[i]["NGAYSINH"].ToString());
            tt.Cauhoibimat = dt.Rows[i]["CAUHOIBIMAT"].ToString();
            tt.Traloichbm = dt.Rows[i]["TRALOICHBM"].ToString();
            tt.Loaitaikhoan = int.Parse(dt.Rows[i]["LOAITAIKHOAN"].ToString());



            return (object)tt;
        }

        public void insert(ThongTinTaiKhoanDTO info)
        {
            string insertCommand = "INSERT INTO THONGTINTAIKHOAN VALUES ('" +
                info.Idtaikhoan + "','" +
                info.Tendangnhap + "','" +
                info.Email + "','" +
                info.Matkhau + "','" +
                info.Quocgia + "','" +
                info.Tinhthanh + "','" +
                info.Gioitinh + "','" +
                info.Ngaysinh + "','" +
                info.Cauhoibimat + "','" +
                info.Traloichbm + "'," +
                info.Loaitaikhoan + ")";

            executeNonQuery(insertCommand);
        }

        public void update(ThongTinTaiKhoanDTO info)
        {
            string updateCommand = "UPDATE THONGTINTAIKHOAN " +
                                    "SET TENDANGNHAP = '" + info.Tendangnhap + "', " +
                                    " EMAIL = '" + info.Email + "'," +
                                    " MATKHAU = '" + info.Matkhau + "'," +
                                    " QUOCGIA = '" + info.Quocgia + "'," +
                                    " TINHTHANH = '" + info.Tinhthanh + "'," +
                                    " GIOITINH = '" + info.Gioitinh + "'," +
                                    " NGAYSINH = '" + info.Ngaysinh + "'," +
                                    " CAUHOIBIMAT = '" + info.Cauhoibimat + "'," +
                                    " TRALOICHBM = '" + info.Traloichbm + "'," +
                                    " LOAITAIKHOAN = " + info.Loaitaikhoan + "," +
                                    " WHERE IDTAIKHOAN  = '" + info.Idtaikhoan + "'";

            executeNonQuery(updateCommand);
        }

        public void delete(string idtk)
        {
            string deleteCommand = "DELETE FROM THONGTINTAIKHOAN WHERE IDTAIKHOAN = '" + idtk + "'";
            executeNonQuery(deleteCommand);
        }
    }
}